/*
 * 文件名：StsMarchCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月23日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.ic.domain.BuildSubDomain;
import top.infopub.job.ic.service.BuildInfoService;
import top.infopub.job.ic.service.BuildSubService;
import top.infopub.job.ic.service.MonthCtrlService;
import top.infopub.job.mkt.service.DiscloseProService;
import top.infopub.job.sts.service.MarchCollectService;
import top.infopub.job.sts.service.StsMarchCollectService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.ic.BuildInfo;
import top.infopub.model.ic.MonthCtrl;
import top.infopub.model.mkt.DisclosePro;
import top.infopub.model.sts.MarchCollect;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


@Service
public class StsMarchCollectServiceImpl implements StsMarchCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private DiscloseProService discloseProService;

    @Autowired
    private MarchCollectService marchCollectService;

    @Autowired
    private BuildInfoService buildInfoService;

    @Autowired
    private MonthCtrlService monthCtrlService;

    @Autowired
    private BuildSubService buildSubService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsMarchCollectJob() {
        //查询所有交底的项目
        Map<String, Object> discloseProWhere = Maps.newHashMap();
        discloseProWhere.put("processStatus", "9");
        List<DisclosePro> disclosePros = discloseProService.selectByMapWhere(discloseProWhere);
        for (DisclosePro disclosePro : disclosePros) {
            MarchCollect marchCollect = new MarchCollect();
            //往表里存放项目信息
            marchCollect.setProCode(disclosePro.getProCode());
//            marchCollect.setProName(disclosePro.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(disclosePro.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                marchCollect.setOrgProName(orgInfos.get(0).getCompName());
                marchCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    marchCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    marchCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            marchCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            marchCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            marchCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //最新的月度管控数据
            Map<String, Object> monthCtrlMap = Maps.newHashMap();
            monthCtrlMap.put("proCode", marchCollect.getProCode());
            monthCtrlMap.put("processStatus", "9");
            monthCtrlMap.put("orderCondition", "ADD_TIME DESC");
            List<MonthCtrl> monthCtrls = monthCtrlService.selectByMapWhere(monthCtrlMap);
            if (CollectionUtils.isNotEmpty(monthCtrls)) {
                MonthCtrl monthCtrl = monthCtrls.get(0);
                //开累计划完成率(%)
                marchCollect.setAddupPlanRate(monthCtrl.getSumPlanRate());
                //实际开竣工管理-已延误天数
                marchCollect.setPutOffDays(monthCtrl.getDelayDays());
                //实际形象进度描述
                marchCollect.setActImageShow(monthCtrl.getActImageShow());
            }
            else {
                continue;
            }
            //投建基本信息
            BuildSubDomain buildSubDomain = buildSubService.selectBuildAllByProCode(marchCollect.getProCode());
            if (buildSubDomain != null) {
                //建设期和工期(合同约定)-开工日
                marchCollect.setOpenDate(buildSubDomain.getBuildLicenceDates());
                //实际开竣工管理-内控开工日
                marchCollect.setCtrlOpenDate(buildSubDomain.getStartDates());
                //实际开竣工管理-内控竣工日期
                marchCollect.setCtrlEndDate(buildSubDomain.getEndDates());
                //实际开竣工管理-停工天数
                marchCollect.setStopDays(buildSubDomain.getStopDay());
                //实际开竣工管理-工期顺延天数
                marchCollect.setDelayedDays(buildSubDomain.getPutOffDay());
                //实际开竣工管理-剩余工期天数
                marchCollect.setRestDays(buildSubDomain.getRestDay());
                //实际开竣工管理-竣工状态
                long currentDate = new Date().getTime();
                Date endDates = buildSubDomain.getEndDates();
                if (endDates != null) {
                    long endDate = endDates.getTime();
                    if (currentDate > endDate) {
                        //已竣工
                        marchCollect.setFinishFlag("1");
                    }
                    else {
                        //未竣工
                        marchCollect.setFinishFlag("0");
                    }
                }
            }
            else {
                continue;
            }
            Map<String, Object> buildMap = Maps.newHashMap();
            buildMap.put("proCode", marchCollect.getProCode());
            buildMap.put("processStatus", "9");
            buildMap.put("orderCondition", "ADD_TIME DESC");
            List<BuildInfo> buildInfos = buildInfoService.selectByMapWhere(buildMap);
            if (CollectionUtils.isNotEmpty(buildInfos)) {
                BuildInfo buildInfo = buildInfos.get(0);
                //建设期和工期(合同约定)-建设期开工时间
                marchCollect.setBuildBeginDate(buildInfo.getBuildStartDate());
                //建设期和工期(合同约定)-建设期结束时间
                marchCollect.setBuildEndDate(buildInfo.getBuildEndDate());
                //建设期和工期(合同约定)-建设期(年)
                marchCollect.setBuildYears(buildInfo.getBuildYears());
            }
            else {
                continue;
            }
            //数据状态(默认为有效)
            marchCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            marchCollect.setAddBy("system");
            marchCollect.setAddTime(new Date());
            marchCollect.setUptBy("system");
            marchCollect.setUptTime(new Date());
            //修改同一个项目下除最新的项目外，其他历史记录数据状态为0(无效的)
            marchCollectService.updateByProCode(marchCollect.getProCode());
            //添加数据
            marchCollectService.insert(marchCollect);
        }
    }
}